import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Scanner;

public class Airline
{
	private ArrayList<Person> people;
	private ArrayList<PassengerRole> passengers;
	private ArrayList<RegularFlight> regularFlights;
	private SimpleDateFormat dateFormat;
	private SimpleDateFormat timeFormat;
	private ConsoleUI ui;
	
	public Airline(boolean useInterface)
	{
		 people = new ArrayList<Person>();
		 regularFlights = new ArrayList<RegularFlight>();
		 passengers = new ArrayList<PassengerRole>();
		 
		 dateFormat = new SimpleDateFormat("dd/MM/yyyy");
		 timeFormat = new SimpleDateFormat("k:m");
		 ui = new ConsoleUI();
		 
		 //initialisation of testing data
		 try
		 {
			 Date date = dateFormat.parse("07/07/2010");
			 addPerson(new Person(1,"Tim Strijdhorst"));
			 addPassenger(new PassengerRole(getPersonFromID(1)));
			 addRegularFlight(new RegularFlight(timeFormat.parse("12:00"), 1, 10));
			 getRegularFlightFromFlightNumber(1).addSpecificFlight(date);
		 }
		 catch(Exception e)
		 {
			 e.printStackTrace();
		 }
		 
		 if(useInterface) handleUserInput();
	}
	
	public static void main(String[] args)
	{
		new Airline(true);
	}
	
	public void handleUserInput()
	{
		boolean run = true;
		
		ui.displayln("Welcome to the Airline System version 0.1337 Alpha");
	    BufferedReader fromConsole = new BufferedReader(new InputStreamReader(System.in));
	    String message;
		
	    try
	    {
			while(run)
			{
				ui.display("command> ");
				message = fromConsole.readLine();
				Scanner in = new Scanner(message);
				
				if(in.hasNext())
				{
					handleCommand(message);
				}
			}
	    }
	    catch(IOException e)
	    {
	    	e.printStackTrace();
	    }
	}
	
	public void handleCommand(String commandString)
	{
		Scanner scanner = new Scanner(commandString);
		ArrayList<String> params = new ArrayList<String>();
		
		//First 'param' is the actual command, the rest are parameters, C style
		while(scanner.hasNext())
		{
			params.add(scanner.next());
		}
		
		String command = params.get(0);
		command = command.toUpperCase();
		
		try
		{
			/**
			 * params[1] == personID
			 * params[2] == RegularFlight	
			 * params[3] == time
			 */
			if(command.equals("BOOK"))
			{
				PassengerRole passenger = getPassengerFromPerson(getPersonFromID(Integer.valueOf(params.get(1))));
				Date date = dateFormat.parse(params.get(3));
				RegularFlight flight = getRegularFlightFromFlightNumber(Integer.valueOf(params.get(2)));
				SpecificFlight specificFlight = flight.getSpecificFlightByDate(date);
				
				if(passenger.makeBooking(flight.getSpecificFlightByDate(date)))
				{
					ui.displayln("Booking successfully made");
				}
				else
				{
					ui.displayln("ERROR: unable to create booking due to the unavailability of free seats");
				}
			}
			
			/**
			 * params[1] == personID
			 * params[2] == RegularFlight
			 * params[3] == date
			 */
			if(command.equals("CHECKBOOKING"))
			{
				PassengerRole passenger = getPassengerFromPerson(getPersonFromID(Integer.valueOf(params.get(1))));
				Date date = dateFormat.parse(params.get(3));
				RegularFlight flight = getRegularFlightFromFlightNumber(Integer.valueOf(params.get(2)));
				if(flight.getSpecificFlightByDate(date).checkIfBookingExists(passenger) != null)
				{
					ui.displayln("Booking exists");
				}
				else
				{
					ui.displayln("Booking does not exist");
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void addPerson(Person person)
	{
		people.add(person);
	}
	
	public void deletePerson(Person person)
	{
		people.remove(people);
	}
	
	public void addPassenger(PassengerRole passenger)
	{
		passengers.add(passenger);
	}
	
	public void deletePassener(PassengerRole passenger)
	{
		passengers.remove(passenger);
	}
	
	public PassengerRole getPassengerFromPerson(Person person)
	{
		for(PassengerRole passenger : passengers)
		{
			if(passenger.getPerson().equals(person))
			{
				return passenger;
			}
		}
		
		return null;
	}
	
	public Person getPersonFromID(int personID)
	{
		for(int i=0;i<people.size();i++)
		{
			if(people.get(i).getIdNumber() == personID)
			{
				return people.get(i);
			}
		}
		
		return null;
	}
	
	public void addRegularFlight(RegularFlight regularFlight)
	{
		regularFlights.add(regularFlight);
	}
	
	public void deleteRegularFlight(RegularFlight regularFlight)
	{
		regularFlights.remove(regularFlight);
	}
	
	public RegularFlight getRegularFlightFromFlightNumber(int flightNumber)
	{
		for(int i=0;i<regularFlights.size();i++)
		{
			if(regularFlights.get(i).getFlightNumber() == flightNumber)
			{
				return regularFlights.get(i);
			}
		}
		
		return null;
	}
}
